<script type="text/javascript">
<!--{literal}
var curr_gd = 0;
var curr_id = 0;

$(document).ready
(
    function()
    {
	$('input:button#verification-act-add-button').click
	(
	    function()
	    {
		$('div.dialog#verification-act-add')
		    .find('.date')
			.val('')
			.end()
		    .dialog('open');
	    }
	);

	$('a.button#verification-act-delete-button').click
	(
	    function()
	    {
		if ( window.confirm('Вы дейстительно хотите удалить акт сверки №'+curr_id+'?') )
		{
		    $.getJSON
		    (
			'verification.php',
			{
			    do:	'delete',
			    id:	curr_id,
			},
			function(json)
			{
			    if ( json['code']==200 )
			    {
				$('table#verification-act-grid-'+$('select#contract-id').val())
				    .trigger('reloadGrid');
			    }
			}
		    );
		}
	    }
	);

	$('div.tabs#verification-act-tabs')
	    .tabs
	    (
		'option',
		{
		    'select':	function(e,ui)
				{
				    curr_grid = $(ui.panel).find('table.grid');

				    if ( cid = $(ui.panel).find('input:hidden.contract-id-value').val() )
				    {
					$('option.contract')
					    .attr('selected',false)
					    .each(function(){if($(this).val()==cid){$(this).attr('selected',true);}});
				    }
				}
		}
	    );

	$('div.dialog#verification-act-add')
	    .dialog
	    (
		'option',
		{
			title:	'Создание акта сверки',
			resizable:	false,
			buttons:
			{
			    'OK':	function()
					{
					    $.getJSON
					    (
						'verification.php',
						{
						    do:			'add',
						    contract_id:	$('select#contract-id').val(),
						    period_start:	$('input:text#verification-act-start-date').val(),
						    period_stop:	$('input:text#verification-act-stop-date').val()
						},
						function(json)
						{
						    if ( json['code']==200 )
						    {
							$('div.dialog#verification-act-add').dialog('close');
							$('table#verification-act-grid-'+$('select#contract-id').val())
							    .trigger('reloadGrid');
						    }
						}
					    );
					}
			}
		}
	    );
    }
);
{/literal}-->
</script>

<div class="dialog" id="verification-act-add">
    <table>
	<tr>
	    <td align="right"><strong>Договор:</strong></td>
	    <td>
		<select id="contract-id">
		{foreach from=$contracts item=contract}
		    <option class="contract" value="{$contract.id}">{$contract.contract_number} ({$contract.executive_name})</option>
		{/foreach}
		</select>
	    </td>
	</tr>
	<tr>
	    <td align="right"><strong>Начало периода:</strong></td>
	    <td>
		<input type="text" class="date" id="verification-act-start-date" value="{$smarty.now|date_format:'%Y-%m-01'}"/>
	    </td>
	</tr>
	<tr>
	    <td align="right"><strong>Конец периода:</strong></td>
	    <td>
		<input type="text" class="date" id="verification-act-stop-date" value="{$smarty.now|date_format:'%Y-%m-01'}"/>
	    </td>
	</tr>
    </table>
</div>

<div class="tabs" id="verification-act-tabs">
    <ul>
{foreach from=$contracts item=contract}
	<li><a href="#verification-act-contract-{$contract.id}">{$contract.executive_name|sslash} ({$contract.contract_number})</a></li>
{/foreach}
    </ul>

{foreach from=$contracts item=contract}
    <script type="text/javascript">
    <!--{literal}
    $(document).ready
    (
	function()
	{
	    $('table#verification-act-grid-{/literal}{$contract.id}{literal}')
		.jqGrid
		({
		    url:	'verification.php?contract_id={/literal}{$contract.id}{literal}',
		    datatype:	'json',
		    sortname:	'id',
		    sortorder:	'desc',
		    colNames:	['Номер','С','По','Дебет','Кредит','Сальдо'],
		    width:	750,
		    height:	600,
		    onSelectRow:	function(id,status)
					{
					    $.getJSON
					    (
						'verification.php',
						{
						    do:	'get',
						    id:	id
						},
						function(json)
						{
						    curr_id = id;

						    $('span#vact-toolbar')
							.find('span#act-number')
							    .text(id)
							    .end()
							.show();

						    if ( json['status']==2 )
						    {
							$('a.button#verification-act-download-button')
							    .attr('href','files/'+json['path'])
							    .attr('title','Скачать акт №'+id)
							    .show();
						    }
						    else
						    {
							$('a.button#verification-act-download-button').hide();
						    }
						}
					    );
					},
		    colModel:
		    [
			{name:'id',width:40},
			{name:'period_start',width:100},
			{name:'period_stop',width:100},
			{name:'turnover_debet',sortable:false},
			{name:'turnover_credit',sortable:false},
			{name:'saldo',sortable:false}
		    ],
		    pager:	'verification-act-grid-{/literal}{$contract.id}{literal}-pager'
		});
	}
    );
    {/literal}-->
    </script>

    <div id="verification-act-contract-{$contract.id}">
	<input type="hidden" class="contract-id-value" value="{$contract.id}"/>
	<table id="verification-act-grid-{$contract.id}" class="grid"></table>
	<div id="verification-act-grid-{$contract.id}-pager"></div>
    </div>
{/foreach}
</div>

<input type="button" class="button" id="verification-act-add-button" value="Новый акт"/>

<span id="vact-toolbar" style="display:none">
    <strong>Активен акт №<span id="act-number"></span></strong>
    <a href="javascript:void(0);" class="button" id="verification-act-delete-button"><img src="images/icons/delete_16.png"/></a>
    <a href="javascript:void(0);" class="button" id="verification-act-download-button" target="_blank"><img src="http://www.paramount.com/webmaster/images/download_icon.png"/></a>
</span>
